package shop.xiaosa.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import shop.xiaosa.mapper.UserMapper;
import shop.xiaosa.pojo.User;
import shop.xiaosa.service.UserService;

import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
@Api(value = "用户")
public class UserController {

    @Autowired
    private UserService userService;


    @RequestMapping("/login")
    @ApiOperation(value = "登陆", notes = "用户登陆")
    public String login(User user) {
        if (user.getPhone() == null || user.getPassword() == null) {
            return "/login/login.html";
        }
        User user1 = userService.login(user.getPhone(), user.getPassword());

        if (user1 != null) {
            return "/login/index.html";
        }
        return "/error/errorLogin.html";
    }

    @RequestMapping("/register")
    @ApiOperation(value = "注册", notes = "用户注册")
    public String register(User user) {
        user.setCreateTime(new Date());
        System.out.println(user.getCreateTime() + "<------"+user.getPhone()+user.getPassword());

        List<User> all = userService.findAll();
        for (User user1 : all) {
            if (user.getPhone().equals(user1.getPhone())) {
                System.out.println("获取的user:" + user.getPhone() + "数据库的user:" +user1.getPhone());
                return "/error/errorLogin.html";
            }
        }
        boolean add = userService.add(user);
        if (add) {
            return "/login/login.html";
        } else {
            return "/error/errorLogin.html";
        }
    }

}
